import { message } from 'ant-design-vue'
import { config, tool } from 'elead-ui-tool-v3'

/**
 * 通用js方法封装处理
 * Copyright (c) 2019 ruoyi
 */
const baseURL = __APP_ENV__.VITE_API_BASEURL
export default {
	//res 为全部文件参数
	//lengthComputable 文件的总大小
	//如果没上面两个参数 , onProgress 将无法计算大小
	async downloadByUrl({ id, type, fileName, success, fail, res, onProgress, lengthComputable }) {
		let isCompleteReached = false // 添加一个标志
		const url = baseURL + '/' + type + '/file/url/' + id
		var xhr = new XMLHttpRequest()
		var formData = new FormData()
		xhr.open('get', url)
		xhr.setRequestHeader('Token', config.TOKEN_PREFIX + tool.data.get('TOKEN'))
		xhr.setRequestHeader('Authorization', config.TOKEN_PREFIX + tool.data.get('TOKEN'))
		xhr.responseType = 'blob'
		xhr.onerror = function() {
			message.destroy()
			message.error('文档下载失败，请联系管理员')
			if (fail) {
				fail()
			}
		}
		xhr.onprogress = (event) => {
			let fileSize = event.total || lengthComputable
			if (fileSize) {
				let percentComplete = Math.round((event.loaded / fileSize) * 100)
				// 检查是否首次达到100%且还没有标记为完成
				if (percentComplete === 100 && !isCompleteReached) {
					onProgress({
						percentComplete
					})
					isCompleteReached = true // 设置标志，表示100%已完成
				}
				// 如果还没达到100%，则正常更新进度
				else if (percentComplete < 100) {
					onProgress({
						percentComplete
					})
				}
			} else {
				onProgress({
					error: '无法获取文件大小'
				})
				console.log('无法获取文件大小,计算进度失败')
			}
		}
		xhr.onload = function(e) {
			if (this.status == 200) {
				var blob = this.response
				if (window.navigator.msSaveOrOpenBlob) {
					navigator.msSaveBlob(blob, fileName)
				} else {
					var a = document.createElement('a')
					var url = window.URL ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob)()
					a.href = url
					a.download = fileName
					document.body.appendChild(a)
					a.click()
					window.URL.revokeObjectURL(url)
				}
				if (success) {
					success()
				}
			} else {
				message.destroy()
				message.error('文档下载失败，请联系管理员')
				if (fail) {
					fail()
				}
			}
		}
		xhr.send(formData)
		return false
	}, async fileGetInfo(id, type, fileName, callback) {
		const url = baseURL + '/' + type + '/file/url/' + id
		callback({
			url,
			token: config.TOKEN_PREFIX + tool.data.get('TOKEN'),
			Authorization: config.TOKEN_PREFIX + tool.data.get('TOKEN'),
			fileName
		})
		return false
	}, async downloadByUrl2(urlParams,fileName) {
		const url = baseURL + urlParams
		var xhr = new XMLHttpRequest()
		var formData = new FormData()
		xhr.open('get', url)
		xhr.setRequestHeader('Token', config.TOKEN_PREFIX + tool.data.get('TOKEN'))
		xhr.setRequestHeader('Authorization', config.TOKEN_PREFIX + tool.data.get('TOKEN'))
		xhr.responseType = 'blob'
		xhr.onload = function(e) {
			if (this.status == 200) {
				var blob = this.response
				if (window.navigator.msSaveOrOpenBlob) {
					navigator.msSaveBlob(blob, fileName)
				} else {
					var a = document.createElement('a')
					var url = window.URL ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob)()
					console.log('url------------------>', url)
					a.href = url
					a.download = fileName
					document.body.appendChild(a)
					a.click()
					window.URL.revokeObjectURL(url)
				}
			} else {
				message.error('文档下载失败，请联系管理员')
			}
		}
		xhr.send(formData)
		return false
	}
}
